package cn.djun.news.service.impl;

import cn.djun.news.dao.UserDao;
import cn.djun.news.entity.User;
import cn.djun.news.service.UserService;
import cn.djun.news.util.Paginating;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author djun
 * @classname UserServiceImpl
 * @description
 * @date 2020/11/24 10:29
 * @email 3170495286@qq.com
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public User getUserById(int id) {
        try {
            User user = userDao.getUserById(id);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public int addUser(User user) {
        try {
            user.setType("0");
            return userDao.insertUser(user);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 判断用户是否存在
     * @param userName
     * @return
     */
    @Override
    public boolean isUserName(String userName) {
        try {
            User user = userDao.getUserByName(userName);
            if (user != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据用户名查询用户
     * @param userName
     * @return
     */
    @Override
    public User getUserByName(String userName) {
        try {
            return userDao.getUserByName(userName);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 分页查询用户
     * @param user
     * @return
     */
    @Override
    public Paginating<User> getUserList(User user) {
        try {
            PageHelper.startPage(user.getPageNum(), user.getPageSize());
            List<User> list = userDao.getUserList(user);
            PageInfo<User> p = new PageInfo<>(list);
            return new Paginating<>(p);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public int updateUser(User user)throws Exception {
        int flag = 0;
        try {
            userDao.updateUser(user);
            flag = 1;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return flag;
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Override
    public int delUser(Integer id) {
        try {
            return userDao.delUser(id);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
